import orderApi from '@/config/server/api/order'

function detail(commissionId) {
  return orderApi.commission.settlement.detail
    .fetch({
      commissionId
    })
    .then((res) => {
      return {
        ...res,
        settlementDetails: res.settlementDetails.map((i) => ({
          ...i,
          settlementAmount: Number(i.settlementAmount),
          baseAgentCost: Number(i.baseAgentCost),
          adjustAmount: Number(i.adjustAmount),
          depositAmount: Number(i.depositAmount),
          settlementDepositAmount: Number(i.settlementDepositAmount)
        }))
      }
    })
}

function adjustList(commissionId) {
  return orderApi.commission.settlement.adjustList.fetch({ commissionId })
}

function adjustSave(info) {
  return orderApi.commission.settlement.adjustSave.fetch(info)
}

// 关系户扣减/确认凭证
function editSettlement(info) {
  return orderApi.commission.settlement.editSettlement.fetch(info)
}

function download(commissionId) {
  return orderApi.commission.settlement.download.fetch({ commissionId })
}

function upload({ commissionId }) {
  let upload = orderApi.commission.settlement.upload
  upload.setData({ commissionId })
  return upload
}

function sendback({ commissionId, backReason }) {
  return orderApi.commission.settlement.sendback.fetch({
    commissionId,
    backReason
  })
}

function save(info) {
  return orderApi.commission.settlement.save.fetch(info)
}

function cancellation(commissionId) {
  return orderApi.commission.settlement.cancellation.fetch({ commissionId })
}

function filterData(list, params) {
  let data = [...list]
  // 房源名称
  if (params.roomName) {
    data = data.filter((i) => i.roomName.indexOf(params.roomName) >= 0)
  }
  // 房源状态
  if (params.commissionOrderStatus) {
    data = data.filter((i) => i.commissionOrderStatus == params.commissionOrderStatus)
  }
  // 签约日期
  if (params.contractDate) {
    let start = new Date(params.contractDate[0]).getTime()
    let end = new Date(params.contractDate[1]).getTime()
    data = data.filter(
      (i) => new Date(i.contractDate).getTime() >= start && new Date(i.contractDate).getTime() <= end
    )
  }
  // 客户姓名
  if (params.customerName) {
    data = data.filter((i) => i.customerName.indexOf(params.customerName) >= 0)
  }
  // 产品
  if (params.productName) {
    data = data.filter((i) => i.productName == params.productName)
  }
  // 业务员
  if (params.counselorName) {
    data = data.filter((i) => i.counselorName.indexOf(params.counselorName) >= 0)
  }
  // 关系户
  if (params.isAssociates) {
    data = data.filter((i) =>
      params.isAssociates === '1' ? i.isAssociates === true : i.isAssociates === false
    )
  }
  // 联代分佣
  if (params.isInsteadOfWorkers) {
    data = data.filter((i) =>
      params.isInsteadOfWorkers === '1' ? i.isInsteadOfWorkers === true : i.isInsteadOfWorkers === false
    )
  }
  // 全款到账日期
  if (params.predictAccountDate) {
    let start = new Date(params.predictAccountDate[0]).getTime()
    let end = new Date(params.predictAccountDate[1]).getTime()
    data = data.filter(
      (i) =>
        new Date(i.predictAccountDate).getTime() >= start && new Date(i.predictAccountDate).getTime() <= end
    )
  }
  // 交房日期
  if (params.dealRoomDate) {
    let start = new Date(params.dealRoomDate[0]).getTime()
    let end = new Date(params.dealRoomDate[1]).getTime()
    data = data.filter(
      (i) => new Date(i.dealRoomDate).getTime() >= start && new Date(i.dealRoomDate).getTime() <= end
    )
  }

  return data
}

export default {
  detail,
  adjustList,
  adjustSave,
  editSettlement,
  download,
  upload,
  sendback,
  save,
  cancellation,
  filterData
}
